package me.aartikov.simplescreenswitchersample.slice;

import me.aartikov.alligator.*;
import me.aartikov.alligator.helpers.FractionCache;
import me.aartikov.alligator.listeners.ScreenSwitchingListener;
import me.aartikov.alligator.screenswitchers.FragmentScreenSwitcher;
import me.aartikov.alligator.Screen;
import me.aartikov.alligator.util.Log;
import me.aartikov.simplescreenswitchersample.ResourceTable;
import me.aartikov.simplescreenswitchersample.SampleApplication;
import me.aartikov.simplescreenswitchersample.screens.TabScreen;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;

import java.util.HashMap;
import java.util.Map;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener, ScreenSwitchingListener {

    private Image icon1;
    private Image icon2;
    private Image icon3;
    private Text textIcon1;
    private Text textIcon2;
    private Text textIcon3;
    private int nowSelect = ResourceTable.Id_openharmony;


    private Navigator mNavigator = SampleApplication.getNavigator();
    private Map<Integer, Screen> mTabScreenMap = new HashMap<>();
    private FragmentScreenSwitcher mScreenSwitcher;
    private NavigationContextBinder mNavigationContextBinder = SampleApplication.getNavigationContextBinder();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

//        componentContainer = (ComponentContainer) LayoutScatter.getInstance(getAbility())
//                .parse(ResourceTable.Layout_ability_main, null, false);
//        setUIContent(componentContainer);
        Log.i("MainAbilitySlice.onStart([intent]):44   " + getAbility() + "   " + ResourceTable.Id_main_container + "    " + ((FractionAbility) getAbility()).getFractionManager());
        initTabScreenMap();
        mScreenSwitcher = new FragmentScreenSwitcher(SampleApplication.getNavigationFactory(),
                ((FractionAbility) getAbility()).getFractionManager());
        bindNavigationContext();

        findComponentById(ResourceTable.Id_openharmony).setClickedListener(this);
        findComponentById(ResourceTable.Id_bug).setClickedListener(this);
        findComponentById(ResourceTable.Id_dog).setClickedListener(this);
        icon1 = (Image) findComponentById(ResourceTable.Id_icon1);
        icon2 = (Image) findComponentById(ResourceTable.Id_icon2);
        icon3 = (Image) findComponentById(ResourceTable.Id_icon3);
        textIcon1 = (Text) findComponentById(ResourceTable.Id_text_icon1);
        textIcon2 = (Text) findComponentById(ResourceTable.Id_text_icon2);
        textIcon3 = (Text) findComponentById(ResourceTable.Id_text_icon3);

        resetBottom();

        Screen screen = getTabScreen(ResourceTable.Id_openharmony);
        mNavigator.switchTo(screen);
    }

    private Screen getTabScreen(int tabId) {
        return mTabScreenMap.get(tabId);
    }

    private void bindNavigationContext() {
        NavigationContext.Builder builder = new NavigationContext.Builder(getAbility(), SampleApplication.getNavigationFactory())
                .screenSwitcher(mScreenSwitcher)
                .screenSwitchingListener(this)
                .fragmentNavigation(((FractionAbility) getAbility()).getFractionManager());  // Use child fragment manager for nested navigation
        mNavigationContextBinder.bind(builder.build());
    }

    private void initTabScreenMap() {
        mTabScreenMap.put(ResourceTable.Id_openharmony, new TabScreen.Ohos(getString(ResourceTable.String_tab_ohos), ResourceTable.Id_main_container, getAbility().toString()));
        mTabScreenMap.put(ResourceTable.Id_bug, new TabScreen.Bug(getString(ResourceTable.String_tab_bug), ResourceTable.Id_main_container, getAbility().toString()));
        mTabScreenMap.put(ResourceTable.Id_dog, new TabScreen.Dog(getString(ResourceTable.String_tab_dog), ResourceTable.Id_main_container, getAbility().toString()));
    }


    private void resetBottom() {
        if (nowSelect == ResourceTable.Id_openharmony) {
            icon1.setImageElement(ImageTint.getImagebyColor(getAbility(), ResourceTable.Media_ic_ohos, Color.RED.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon1.setTextColor(Color.RED);
        } else {
            icon1.setImageElement(ImageTint.getImagebyColor(getAbility(), ResourceTable.Media_ic_ohos, Color.BLACK.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon1.setTextColor(Color.BLACK);
        }

        if (nowSelect == ResourceTable.Id_bug) {
            icon2.setImageElement(ImageTint.getImagebyColor(getAbility(), ResourceTable.Media_ic_bug, Color.RED.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon2.setTextColor(Color.RED);
        } else {
            icon2.setImageElement(ImageTint.getImagebyColor(getAbility(), ResourceTable.Media_ic_bug, Color.BLACK.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon2.setTextColor(Color.BLACK);
        }

        if (nowSelect == ResourceTable.Id_dog) {
            icon3.setImageElement(ImageTint.getImagebyColor(getAbility(), ResourceTable.Media_ic_dog, Color.RED.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon3.setTextColor(Color.RED);
        } else {
            icon3.setImageElement(ImageTint.getImagebyColor(getAbility(), ResourceTable.Media_ic_dog, Color.BLACK.getValue(), AttrHelper.vp2px(30, this), AttrHelper.vp2px(30, this)));
            textIcon3.setTextColor(Color.BLACK);
        }
    }


    @Override
    public void onActive() {
        super.onActive();
    }


    @Override
    public void onClick(Component component) {
        Screen screen = getTabScreen(component.getId());
        mNavigator.switchTo(screen);
        switch (component.getId()) {
            case ResourceTable.Id_openharmony:
            case ResourceTable.Id_bug:
            case ResourceTable.Id_dog:
                nowSelect = component.getId();
                break;
        }
        resetBottom();
    }

    @Override
    public void onScreenSwitched(Screen screenFrom, Screen screenTo) {
        bindNavigationContext();
    }

    @Override
    protected void onBackPressed() {
        FractionCache.getInstance().clear();
        super.onBackPressed();
    }
}
